package com.milk.controller.user;


import com.milk.dto.OrderDTO;
import com.milk.dto.ShoppingCarDTO;
import com.milk.entity.Orders;
import com.milk.result.Result;
import com.milk.service.IOrdersService;
import com.milk.vo.OrderDetailVO;
import com.milk.vo.OrderVO;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/order")
@Slf4j
public class OrderController {
    @Autowired
    private IOrdersService ordersService;


    /**
     * 用户点击结算
     */
    @GetMapping
    @ApiModelProperty("用户点击结算")
    public Result<OrderVO> settle(){
        OrderVO vo = ordersService.settle();
        return Result.success(vo);
    }

    /**
     * 下单
     * @param dto
     * @return
     */
    @PostMapping
    @ApiModelProperty("下单")
    public Result<Integer> placeOrder(@RequestBody OrderDTO dto){
        log.info("下单信息：{}",dto);
        return Result.success(ordersService.placeOrder(dto));
    }

    /**
     * 查看今日订单
     * @return
     */
    @GetMapping("/today")
    @ApiOperation("今日订单")
    public Result<List<OrderDetailVO>> todayOrder(){
        List<OrderDetailVO> list = ordersService.selectTodayOrder();
        return Result.success(list);
    }

    /**
     * 查看历史订单
     * @return
     */
    @GetMapping("/history")
    @ApiOperation("查看历史订单")
    public Result<List<OrderDetailVO>> historyOrder(){
        List<OrderDetailVO> list = ordersService.selectHistory();
        return Result.success(list);
    }

    /**
     * 根据订单Id查询订单明细
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<OrderDetailVO> selectByOrderId(@PathVariable Long id){
        log.info("订单ID = {}",id );
        OrderDetailVO vo = ordersService.selectByOrderId(id);
        return Result.success(vo);
    }

    /**
     * 直接点击购买
     * @param dto
     * @return
     */
    @PostMapping("/buyOne")
    public Result<OrderVO> buyOne(@RequestBody ShoppingCarDTO dto){
        log.info("购买的商品信息 = {}",dto);
        OrderVO vo = ordersService.buyOne(dto);
        return Result.success(vo);
    }

    /**
     * 查询前方还有多少订单
     * @return
     */
    @GetMapping("/making")
    public Long makingCount(){
        return ordersService.selectMaking();
    }

}
